﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FGrubert.CrmQueueMonitor.WinForms.InfoMonitor
{
    /// <summary>
    /// The settings window for a single information form
    /// </summary>
    public partial class FrmSettings : Form
    {
        /// <summary>
        /// The list of info items
        /// </summary>
        private List<InfoItem> _infoItems;
        /// <summary>
        /// The configuration of the window
        /// </summary>
        private WindowConfiguration _config;
        /// <summary>
        /// Gets the configuration of the window
        /// </summary>
        public WindowConfiguration Config
        {
            get { return _config; }
        }

        /// <summary>
        /// Create a new settings window
        /// </summary>
        /// <param name="config">The configuration to show</param>
        public FrmSettings(WindowConfiguration config)
        {
            InitializeComponent();

            this._config = config;
            txtWindowTitle.Text = _config.WindowTitle;
            numRefreshInterval.Value = _config.RefreshInterval;
            chkAlwaysOnTop.Checked = _config.AlwaysOnTop;
            // Copy the list of info items so it is not edited directly but you can abort
            _infoItems = new List<InfoItem>(_config.Infos.Count);
            foreach (InfoItem oldItem in _config.Infos)
            {
                _infoItems.Add(oldItem.DeepCopy());
            }
            dgvInfos.DataSource = new BindingList<InfoItem>(_infoItems);
        }

        /// <summary>
        /// Save the current settings and close the window
        /// </summary>
        private void btnOK_Click(object sender, EventArgs e)
        {
            _config.WindowTitle = txtWindowTitle.Text;
            _config.RefreshInterval = Convert.ToInt32(numRefreshInterval.Value);
            _config.AlwaysOnTop = chkAlwaysOnTop.Checked;
            _config.Infos.Clear();
            _config.Infos.AddRange(_infoItems);
        }
    }
}
